<template>
  <div style="padding: 20px; max-width: 800px; margin: 0 auto">
    <h1 style="text-align: center">项目：任务标记</h1>

    <!-- 添加按钮 -->
    <el-button
      type="primary"
      style="margin: 20px auto; display: block"
      @click="openAddDialog"
      >添加</el-button
    >

    <!-- 表格展示任务列表 -->
    <el-table :data="tasks" style="width: 80%; margin: 0 auto">
      <el-table-column prop="id" label="任务编号"></el-table-column>
      <el-table-column prop="title" label="任务标题"></el-table-column>
      <el-table-column prop="mark" label="任务状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.mark"
            @change="handleMarkChange(scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click="deleteTask(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加对话框 -->
    <el-dialog title="添加任务" :visible.sync="addDialogVisible">
      <el-form :model="newTask">
        <el-form-item label="任务标题">
          <el-input v-model="newTask.title"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addTask">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "@/http/request";

export default {
  data() {
    return {
      addDialogVisible: false, // 添加对话框

      tasks: [],
      newTask: {
        title: "",
      },
    };
  },
  methods: {
    async updateTaskMark(id, mark) {
      let resp = await axios({
        method: "get",
        url: "/task?type=updateTask",
        params: { id: id, mark: mark ? 1 : 0 },
      });
      console.log(resp);
      if (resp === "success") {
        this.$message({
          message: `任务 ${id} 的状态已更新为 ${mark}`,
          type: "success",
        });
      } else {
        this.$message({
          message: `任务 ${id} 的状态更新失败`,
          type: "error",
        });
      }
    },
    async addTask() {
      let resp = await axios({
        method: "post",
        url: "/task?type=addTask",
        data: this.newTask,
      });
      console.log(resp);
      if (resp === "success") {
        this.$message({
          message: `任务已添加`,
          type: "success",
        });
      } else {
        this.$message({
          message: `任务添加失败`,
          type: "error",
        });
      }
      this.getTasks();
      this.addDialogVisible = false;
      this.newTask = { title: "" };
    },

    async deleteTask(id) {
      let resp = await axios({
        method: "get",
        url: "/task?type=deleteTask",
        params: { id: id },
      });
      console.log(resp);
      if (resp === "success") {
        this.$message({
          message: `任务 ${id} 已删除`,
          type: "success",
        });
      } else {
        this.$message({
          message: `任务 ${id} 删除失败`,
          type: "error",
        });
      }
      this.getTasks();
    },

    handleMarkChange(row) {
      const statusText = row.mark ? "已标记" : "未标记";
      this.$message({
        message: `任务 ${row.id} 的状态已更新为 ${statusText}`,
        type: "success",
      });
      this.updateTaskMark(row.id, row.mark);
    },

    openAddDialog() {
      this.addDialogVisible = true;
    },
    async getTasks() {
      let resp = await axios({
        method: "get",
        url: "/task?type=getTasks",
        params: {},
      });
      console.log(resp);
      this.tasks = resp.map((item) => ({
        ...item,
        mark: item.mark === 1, // 转成布尔值：1 → true, 0 → false
      }));
      //   this.tasks = resp; //获取到后端所有任务数据 赋值给tasks    会把前面的处理结果覆盖掉！
    },
  },
  created() {
    this.getTasks();
  },
};
</script>

<style scoped></style>
